<?php
declare(strict_types=1);

namespace SixShop\Message\Entity;

use think\Model;

/**
 * 消息设置实体类
 */
class MessageSettingEntity extends Model
{
    // 设置表名
    protected $name = 'message_settings';
    
    // 设置字段信息
    protected $schema = [
        'id'               => 'int',
        'user_id'          => 'int',
        'system_notify'    => 'int',
        'activity_notify'  => 'int',
        'order_notify'     => 'int',
        'logistics_notify' => 'int',
        'private_message'  => 'int',
        'create_time'      => 'int',
        'update_time'      => 'int',
    ];
    
    // 自动时间戳
    protected $autoWriteTimestamp = true;
    
    /**
     * 关联用户
     */
    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id');
    }
    
    /**
     * 获取系统通知状态文本
     */
    public function getSystemNotifyTextAttr()
    {
        return $this->system_notify ? '开启' : '关闭';
    }
    
    /**
     * 获取活动通知状态文本
     */
    public function getActivityNotifyTextAttr()
    {
        return $this->activity_notify ? '开启' : '关闭';
    }
    
    /**
     * 获取订单通知状态文本
     */
    public function getOrderNotifyTextAttr()
    {
        return $this->order_notify ? '开启' : '关闭';
    }
    
    /**
     * 获取物流通知状态文本
     */
    public function getLogisticsNotifyTextAttr()
    {
        return $this->logistics_notify ? '开启' : '关闭';
    }
    
    /**
     * 获取私信状态文本
     */
    public function getPrivateMessageTextAttr()
    {
        return $this->private_message ? '开启' : '关闭';
    }
    
    /**
     * 检查指定类型的通知是否开启
     *
     * @param int $type 通知类型：1-系统通知，2-活动通知，3-订单通知，4-物流通知，5-私信
     * @return bool
     */
    public function isNotifyEnabled($type)
    {
        switch ($type) {
            case 1:
                return (bool)$this->system_notify;
            case 2:
                return (bool)$this->activity_notify;
            case 3:
                return (bool)$this->order_notify;
            case 4:
                return (bool)$this->logistics_notify;
            case 5:
                return (bool)$this->private_message;
            default:
                return true;
        }
    }
}
